package com.lory.service.impl;

import com.lory.pojo.Order;
import com.lory.pojo.Product;
import com.lory.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "111", "上海", 5555d, selectProductListByLoadBalancerAnnotation());
    }


    private List<Product> selectProductListByLoadBalancerAnnotation(){
        ResponseEntity<List<Product>> response = restTemplate.exchange(
                "http://service-provider/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Product>>() {
                }
        );
        return response.getBody();
    }
}
